package Actions;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Scanner;

import javax.swing.JFileChooser;

import view.ScriptEditor;
import view.ViewController;
import view.slogoFile;

public class FileEditScript extends AbstractActionPerformed {

	private JFileChooser fileChoose = new JFileChooser(System.getProperty("user.dir"));
	
	@Override
	public void action(ViewController listener, ActionEvent e) {
		fileChoose.setFileFilter(new slogoFile());
		int chosen = fileChoose.showOpenDialog(null);
		
		if (chosen == JFileChooser.APPROVE_OPTION)
		{
			File f = fileChoose.getSelectedFile();
			ScriptEditor editor = new ScriptEditor(f.getName(), listener);
			listener.setChildFrame(editor);
			
			try {
				Scanner s = new Scanner(f).useDelimiter("\\Z");
				editor.setContents(s.next());
			}
			catch (Exception ex){
				ex.printStackTrace();
			}
			
		}
	}

}
